package com.jy.wms.controller;

import PTLwebService.CheckInventory;
import PTLwebService.SendCheckInventoryData;
import PTLwebService.SendCheckInventoryDataResponse;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.BlacklogPlan;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsInventoryTask;
import com.jy.wms.service.CxfInventoryPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.pojo.WmsInventoryPlan;
import com.jy.wms.service.WmsInventoryPlanService;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsInventoryPlanController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Tue Jan 09 10:16:55 CST 2018
 */
@Controller
@RequestMapping("/wmsinventoryplan")
@Api(value = "/wmsinventoryplan", description = "盘点计划表")
public class  WmsInventoryPlanController extends BaseController<WmsInventoryPlanService,WmsInventoryPlan> {

    @Autowired
    private CxfInventoryPlanService cxfInventoryPlanService;

    /**
     * 查询盘点计划信息
     * @param entity
     * @return
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "查询盘点计划信息",response = JSONObject.class, notes = "查询盘点计划信息")
    public @ResponseBody JSONObject getWmsInventoryPlan(@RequestBody WmsInventoryPlan entity,HttpSession session) {
        Page<WmsInventoryPlan> list = new Page<WmsInventoryPlan>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            list=baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 新增盘点计划
     * @param entity
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增盘点计划",response = JSONObject.class, notes = "新增盘点计划")
    public @ResponseBody JSONObject addWmsInventoryPlan(@RequestBody WmsInventoryPlan entity,HttpSession session) {
        try {
            entity.setId(UUIDUtils.uuid());
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            baseService.baseInsert(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增盘点计划信息成功","",  JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增盘点计划信息失败", e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增盘点计划信息失败", e.getMessage(), "");
        }
    }
    /**
     * 盘点计划预检
     * @param id
     * @return
     */
    @GetMapping(value = "/planWarning/{id}")
    @ApiOperation(value = "盘点计划预检",response = JSONObject.class, notes = "盘点计划预检")
    public @ResponseBody JSONObject planWaring(@PathVariable("id") String id,HttpSession session) {
        try {
            baseService.planWaring(id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"盘点计划预检成功","", "");
        }catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"盘点计划预检成功",e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "盘点计划预检失败", e.getMessage(), "");
        }
    }
    /**
     * 关闭盘点
     * @param id
     * @return
     */
    @GetMapping(value = "/closePlan/{id}")
    @ApiOperation(value = "关闭盘点",response = JSONObject.class, notes = "关闭盘点")
    public @ResponseBody JSONObject closePlan(@PathVariable("id") String id,HttpSession session) {
        try {
            baseService.closePlan(id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"关闭盘点计划成功","", "");
        }catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.error.toString(),"关闭盘点计划失败",e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "关闭盘点计划失败", e.getMessage(), "");
        }
    }
    /**
     * 生成盘点任务
     * @param id
     * @return
     */
    @GetMapping(value = "/addPlanRw/{id}")
    @ApiOperation(value = "生成盘点任务",response = JSONObject.class, notes = "生成盘点任务")
    public @ResponseBody JSONObject addPlanRw(@PathVariable("id") String id,HttpSession session) {
        try {
            baseService.addPlanRw(id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"生成盘点任务成功","", "");
        }catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.error.toString(),"生成盘点任务失败",e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "生成盘点任务失败", e.getMessage(), "");
        }
    }
    /**
     * 取消盘点任务
     * @param id
     * @return
     */
    @GetMapping(value = "/closePlanRw/{id}")
    @ApiOperation(value = "取消盘点任务",response = JSONObject.class, notes = "取消盘点任务")
    public @ResponseBody JSONObject closePlanRw(@PathVariable("id") String id,HttpSession session) {
        try {
            baseService.closePlanRw(id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"取消盘点任务成功","", "");
        }catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.error.toString(),"取消盘点任务失败",e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "取消盘点任务失败", e.getMessage(), "");
        }
    }
    /**
     * 库存盘点
     * @param list
     * @return
     */
    @PostMapping(value = "/addStockPlan")
    @ApiOperation(value = "库存盘点",response = JSONObject.class, notes = "库存盘点")
    public @ResponseBody JSONObject addStockPlan(@RequestBody List<WmsInventoryTask> list,HttpSession session) {
        try {
            baseService.addStockPlan(list);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"库存盘点成功","", "");
        }catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"库存盘点失败",e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "库存盘点失败", e.getMessage(), "");
        }
    }
    /**
     * 查看库存盘点信息
     * @param entity
     * @return
     */
    @PostMapping(value = "getStockPlan")
    @ApiOperation(value = "查看库存盘点信息",response = JSONObject.class, notes = "GET查看库存盘点信息")
    public @ResponseBody JSONObject getWmsInventoryPlanById(@RequestBody WmsInventoryTask entity) {
        List<WmsInventoryTask> list = new Page<>();
        JSONObject json = getJSONObject();
        try{
             list=baseService.getStockPlan(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 生成库存调整单信息
     * @param id
     * @return
     */
    @GetMapping(value = "getStockCyRw/{id}")
    @ApiOperation(value = "生成库存调整单信息",response = JSONObject.class, notes = "GET生成库存调整单信息")
    public @ResponseBody JSONObject getStockCyRw(@PathVariable("id") String id) {
        List<WmsInventoryTask> list = new ArrayList<WmsInventoryTask>();
        JSONObject json = getJSONObject();
        try{
            list=baseService.getStockCyRw(id);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (ServiceException e){
            e.printStackTrace();
            json=SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"生成库存调整单失败",e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            json=SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"生成库存调整单失败",e.getMessage(), "");
        }
        return json;
    }
    /**
     * 调整库存
     * @param session
     * @return
     */
    @PostMapping(value = "/tzPlanRw")
    @ApiOperation(value = "调整库存",response = JSONObject.class, notes = "调整库存")
    public @ResponseBody JSONObject tzPlanRw(@RequestBody SetAndModifyModel setModel,HttpSession session) {
        try {
            baseService.tzPlanRw(setModel.getIds());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"调整库存成功","", "");
        }catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"调整库存失败",e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "调整库存失败", e.getMessage(), "");
        }
    }
    /**
     * 打印盘点任务清单
     * @param entity
     * @return
     */
    @PostMapping(value = "printPlanRw")
    @ApiOperation(value = "打印盘点任务清单",response = JSONObject.class, notes = "GET打印盘点任务清单")
    public @ResponseBody JSONObject printPlanRw(@RequestBody WmsInventoryTask entity,HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            //获取当前用户
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ry=userinfo.getUser().getName();
            json=baseService.printPlanRw(entity,ry);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 盘点任务下发_Jane
     * @param map
     * @returnm
     */
    @PostMapping(value = "/issuePlanRw")
    @ApiOperation(value = "盘点任务下发",response = JSONObject.class, notes = "GET盘点任务下发")
    public @ResponseBody JSONObject issuePlanRw(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid=hz.get(0).getHzid();
             json=baseService.addCxfInventoryPlan(map.get("id").toString(),sshzid,map.get("zyryid").toString());
             if((!"".equals(json.get("pda".toString())))&&(!"".equals(json.get("dps".toString())))){
                 return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"pad和dps下发任务成功","", "");
             }else if(!"".equals(json.get("pda".toString()))){
                 return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"Pad下发任务成功","", "");

             }else if(!"".equals(json.get("dps".toString()))){
                 return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"dps下发任务成功","", "");
             }
        }catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "盘点任务下发失败", e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "盘点任务下发失败", e.getMessage(), "");
        }
        return json;
    }
    /**
     * 盘点计划回传_Jane
     * @param blacklogPlan
     * @return
     */
    @PostMapping(value = "backlogPlan")
    @ApiOperation(value = "下发盘点计划回传信息",response = JSONObject.class, notes = "下发盘点计划回传信息")
    public @ResponseBody JSONObject backlogPlan(@RequestBody BlacklogPlan blacklogPlan) {
        JSONObject json = getJSONObject();
        try{
             baseService.backlogPlan(blacklogPlan);
                json=SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"下发盘点计划回传信息成功","", "");

        }catch (ServiceException e){
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "下发盘点计划回传信息失败", e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "下发盘点计划回传信息失败", e.getMessage(), "");
        }
        return json;
    }
    /**
     * 打印盘点损益单
     * @param entity
     * @return
     */
    @PostMapping(value = "/printSYPlanRw")
    @ApiOperation(value = "打印盘点损益单",response = JSONObject.class, notes = "打印盘点损益单")
    public @ResponseBody JSONObject printSYPlanRw(@RequestBody WmsInventoryTask entity,HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            //获取当前用户
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ry=userinfo.getUser().getName();
            json=baseService.printSYPlanRw(entity,ry);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 波次号生成
     * @param session
     * @return
     */
    @ApiOperation(value = "盘点计划 生成盘点编号",response = JSONObject.class, notes = "GET生成盘点编号")
    @GetMapping(value = "getPdBh")
    public @ResponseBody String getBcBh(HttpSession session) {
        String bcBh = null;
        try {
            /**
             * 获取当前仓库
             */
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            /**
             * 取仓库id ckid
             */
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            /**
             * 获取当前货主id  owner
             */
            String owner = null;
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                owner = hz.get(0).getHzid();
            }
            bcBh = baseService.creatPdBh(ckid, owner);
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bcBh;
    }
}
